package com.website.company.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.website.company.entity.Person;
import com.website.company.entity.Product;
import com.website.company.mapper.PersonMapper;
import com.website.company.mapper.ProductClassMapper;
import com.website.company.mapper.ProductMapper;
import com.website.company.service.PersonService;
import com.website.company.service.ProductService;
import com.website.company.utils.Code;
import com.website.company.utils.ProductSearchDTO;
import com.website.company.utils.ResponseList;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author peng yangyan
 * @since 2018-11-22
 */
@Service
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements PersonService {


    @Override
    public void add(Person person) {
        person.setCreateDatetime(new Date());
        person.setUpdateDatetime(new Date());
//        ProductClass personClass = personClassMapper.selectById(person.getProductClassId());
//        person.setProductClassName(personClass.getName());
        baseMapper.insert(person);
    }

    @Override
    public void update(Person person) {
        person.setUpdateDatetime(new Date());
        baseMapper.updateById(person);
    }

    @Override
    public void delete(Integer[] ids) {
        baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public Person show(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public ResponseList<Person> list(Integer page, Integer rows, String keywords, String personClassName) {
        EntityWrapper<Person> entityWrapper = new EntityWrapper<>();
        entityWrapper.orderBy("paixu", true);
        int count = baseMapper.selectCount(entityWrapper);
        ResponseList<Person> responses = new ResponseList<>();
        if (!StringUtils.isEmpty(keywords)) {
            String[] split = keywords.split("&");
            Arrays.asList(split).forEach(v -> entityWrapper.like("keywords", v));
        }
        if (!StringUtils.isEmpty(personClassName)) {
            entityWrapper.eq("person_class_name", personClassName);
        }
        List<Person> news = baseMapper.selectPage(new Page<Product>(page, rows), entityWrapper);
        responses.setRows(rows);
        responses.setPage(page);
        responses.setTotal(count);
        responses.setResult(news);
        responses.setTotalPage(count / rows + 1);
        responses.setCode(Code.SUCCESSED);
        responses.setMsg("操作成功");
        return responses;
    }

    @Override
    public ResponseList<Person> listByOne(Integer page, Integer rows, String keywords, String personClassName) {
        EntityWrapper<Person> entityWrapper = new EntityWrapper<>();
//        entityWrapper.eq("person_class_name", personClassName);
        entityWrapper.orderBy("paixu", true);
        int count = baseMapper.selectCount(entityWrapper);
        ResponseList<Person> responses = new ResponseList<>();
        if (!StringUtils.isEmpty(keywords)) {
            String[] split = keywords.split("&");
            Arrays.asList(split).forEach(v -> entityWrapper.like("keywords", v));
        }
        List<Person> news = baseMapper.selectPage(new Page<Person>(page, rows), entityWrapper);
        responses.setRows(rows);
        responses.setPage(page);
        responses.setTotal(count);
        responses.setResult(news);
        responses.setTotalPage(count / rows + 1);
        responses.setCode(Code.SUCCESSED);
        responses.setMsg("操作成功");
        return responses;
    }

    @Override
    public List<Person> allPerson() {
        EntityWrapper<Person> entityWrapper = new EntityWrapper<>();
        entityWrapper.orderBy("update_datetime", false);
        return baseMapper.selectList(entityWrapper);
    }


}
